package com.wecash.google.translate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wecash.util.HttpRequestUtils;
import org.apache.http.HttpException;
import org.apache.http.client.utils.URLEncodedUtils;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;

/**
 * Created by DK on 17/7/9 21:57.
 * doc:
 */
public class TranslateUtil {
    private static ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");

    static String url = "https://translate.google.cn/translate_a/single?client=t&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=2&ssel=6&tsel=3&kc=1&tk=";
    public static String translateChinese2English(String chinese) throws Exception {
        String newUlr = url+tk(chinese)+"&q="+URLEncoder.encode(chinese,"UTF-8");
        String result = HttpRequestUtils.doGet(newUlr);
        System.out.println(result);
        JSONArray jsonArray = JSON.parseArray(result);
        JSONArray targetArray = jsonArray.getJSONArray(0).getJSONArray(0);
        System.out.println(targetArray.get(0));
        System.out.println(targetArray.get(1));
        return targetArray.get(0).toString();
        //
    }


    public static void main(String[] args) throws Exception {
        String chinese = "坐席电话";
       String result =  translateChinese2English(chinese);
        System.out.println(chinese + "to english is : " + result);

    }

    private static String tk(String val) throws Exception{
        String script ="function tk(a) {"
                +"var TKK = ((function() {var a = 561666268;var b = 1526272306;return 406398 + '.' + (a + b); })());\n"
                +"function b(a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = 'a' <= c ? c.charCodeAt(0) - 87 : Number(c), c = '+' == b.charAt(d + 1) ? a >>> c : a << c; a = '+' == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a }\n"
                +"for (var e = TKK.split('.'), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {"
                +"var c = a.charCodeAt(f);"
                +"128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)"
                +"}"
                +"a = h;"
                +"for (d = 0; d < g.length; d++) a += g[d], a = b(a, '+-a^+6');"
                +"a = b(a, '+-3^+b+-f');"
                +"a ^= Number(e[1]) || 0;"
                +"0 > a && (a = (a & 2147483647) + 2147483648);"
                +"a %= 1E6;"
                +"return a.toString() + '.' + (a ^ h)\n"
                +"}";

        engine.eval(script);
        Invocable inv = (Invocable) engine;
        return (String) inv.invokeFunction("tk", val);
    }
}
